﻿

@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService

<MDialog @bind-Value="Dialog"
         Width="800">
    <MDCard>
        <MCardTitle Class="text-h5">
            创建收藏夹
        </MCardTitle>
        <MCardText>
            公开的收藏夹也可用作“目录”、“合集”......
        </MCardText>
        <MCardText Class="pb-0">
            <div style="color: black;">
                <CnGalWebSite.Components.Containers.MasaTitleContainer Icon="@IconType.Image.ToIconString()" Title="图片" Class="mb-4" Collapable Outline OutlineHead>
                    <MRow>
                        <MCol Cols="12" Md="6">
                            <CnGalWebSite.Components.Images.UploadButton ShowCropper @bind-ImagePath="@Model.MainImage" Type="ImageAspectType._16_9" Text="上传主图" />
                        </MCol>
                    </MRow>
                </CnGalWebSite.Components.Containers.MasaTitleContainer>
                <CnGalWebSite.Components.Containers.MasaTitleContainer Icon="@IconType.Home.ToIconString()" Title="信息" Class="mb-4" Collapable Outline OutlineHead>
                    <MRow>
                        <MCol Cols="12" Md="6">
                            <MTextField @bind-Value="Model.Name" Label="名称" />
                        </MCol>
                        <MCol Cols="12">
                            <MTextarea Rows="3" @bind-Value="Model.BriefIntroduction" Label="简介" />
                        </MCol>
                          <MCol Cols="12" Md="4">
                            <MCheckbox @bind-Value="Model.ShowPublicly" Label="公开" Class="m-0" HideDetails="true" />
                        </MCol>
                          <MCol Cols="12" Md="4">
                            <MCheckbox @bind-Value="Model.IsDefault" Label="默认收藏夹" Class="m-0" HideDetails="true" />
                        </MCol>
                        <MCol Cols="12" Md="4">
                            <MCheckbox @bind-Value="Model.IsHidden" Label="不展示在个人主页中" Class="m-0" HideDetails="true" />
                        </MCol>
                    </MRow>
                </CnGalWebSite.Components.Containers.MasaTitleContainer>
            </div>
        </MCardText>


        <MCardActions>
            <MSpacer></MSpacer>
            <CnGalWebSite.Components.Buttons.MasaButton Text="创建" IsAsync="true" OnClick="OnCreate" Icon="mdi-check" Color="success" TextStyle />
        </MCardActions>
    </MDCard>
</MDialog>
@code {

    [Parameter]
    public string UserId { get; set; }
    [Parameter]
    public EventCallback OnCreated { get; set; }

    bool Dialog { get; set; }

    CreateFavoriteFolderViewModel Model = new CreateFavoriteFolderViewModel();


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public async Task OnCreate()
    {
        try
        {
            var obj = await _httpService.PostAsync<CreateFavoriteFolderViewModel, Result>("api/favorites/CreateFavoriteFolder", Model);


            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("创建收藏夹失败", obj.Error);
            }
            else
            {

                await PopupService.ToastSuccessAsync("创建收藏夹成功", "创建收藏夹成功");
                Dialog = false;
                StateHasChanged();
                await  OnCreated.InvokeAsync();

            }

        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "创建收藏夹失败");
        }
    }

    public void Refrsh()
    {
        Dialog = true;
        StateHasChanged();
        
    }
}
